package org.road0329.dlt698.parse;

import com.alibaba.fastjson2.JSON;
import org.road0329.dlt698.DataUtils;
import org.road0329.dlt698.axdr.*;
import org.road0329.dlt698.bean.FrameData;
import org.road0329.dlt698.tools.ParseTool;
import org.road0329.dlt698.tools.ResultCode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ParseActionResponse {
    public static String parseActionResponseNormal(ActionResponseNormal actionResponseNormal) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> dataOption = new HashMap<>();
        FrameData frameData = new FrameData();

        result.put("dar", DataUtils.bytesToHexString(actionResponseNormal.getResult().getDar().getDataCode(), 0, actionResponseNormal.getResult().getDar().getDataCode().length));
        if (actionResponseNormal.getResult().getRetData().isUsed()) {
            dataOption.put("data", ParseTool.parseData(actionResponseNormal.getResult().getRetData().getValue()));
        } else {
            dataOption.put("data", new HashMap<>());
        }
        result.put("data_option", dataOption);

        frameData.setCode(ResultCode.SUCCESS.getCode());
        frameData.setData(result);
        return JSON.toJSONString(frameData);
    }

    public static String parseActionResponseNormalList(ActionResponseNormalList actionResponseList) {
        Map<String, Object> actionResponseListMap = new HashMap<>();
        List<Map<String, Object>> resultArray = new ArrayList<>();
        FrameData frameData = new FrameData();

        for (int i = 0; i < actionResponseList.getResults().size(); i++) {
            Map<String, Object> result = new HashMap<>();
            Map<String, Object> dataOption = new HashMap<>();
            RETOMDDATA retOmdData = actionResponseList.getResults().get(i);

            result.put("dar", DataUtils.bytesToHexString(retOmdData.getDar().getDataCode(), 0, retOmdData.getDar().getDataCode().length));
            if (retOmdData.getRetData().isUsed()) {
                dataOption.put("data", ParseTool.parseData(retOmdData.getRetData().getValue()));
            } else {
                dataOption.put("data", new HashMap<>());
            }
            result.put("data_option", dataOption);
            resultArray.add(result);
        }
        actionResponseListMap.put("seq_of_result", resultArray);

        frameData.setCode(ResultCode.SUCCESS.getCode());
        frameData.setData(actionResponseListMap);
        return JSON.toJSONString(frameData);
    }

    public static String parseActionThenGetResponseList(ActionThenGetResponseNormalList actionThenGet) {
        FrameData frameData = new FrameData();
        Map<String, Object> actionThenGetListMap = new HashMap<>();
        List<Map<String, Object>> resultArray = new ArrayList<>();

        for (int i = 0; i < actionThenGet.getReads().size(); i++) {
            Map<String, Object> proxyActionThenGet = new HashMap<>();
            Map<String, Object> dataOption = new HashMap<>();
            Map<String, Object> get_result = new HashMap<>();
            READRETOMDDATA readOmdData = actionThenGet.getReads().get(i);

            proxyActionThenGet.put("dar", DataUtils.bytesToHexString(readOmdData.getDar().getDataCode(), 0, readOmdData.getDar().getDataCode().length));
            if (readOmdData.getRetData().isUsed()) {
                dataOption.put("data", ParseTool.parseData(readOmdData.getRetData().getValue()));
            } else {
                dataOption.put("data", new HashMap<>());
            }
            proxyActionThenGet.put("data_option", dataOption);

            GetResult getResult = readOmdData.getResult();
            switch (getResult.getChoiceIndex().getValue()) {
                case 0:
                    Map<String, Object> dar = new HashMap<>();
                    dar.put("dar", DataUtils.bytesToHexString(getResult.getDar().getDataCode(), 0, getResult.getDar().getDataCode().length));
                    proxyActionThenGet.put("get_result", dar);
                    break;
                case 1:
                    proxyActionThenGet.put("get_result", ParseTool.parseData(getResult.getData()));
                    break;
            }
            resultArray.add(proxyActionThenGet);
        }
        actionThenGetListMap.put("seq_of_result", resultArray);

        frameData.setCode(ResultCode.SUCCESS.getCode());
        frameData.setData(actionThenGetListMap);
        return JSON.toJSONString(frameData);
    }
}
